<template>
	<view class="content">
		<z-paging ref="paging" v-model="policeList" @query="queryList">
			<view class="search-bar">
				<uni-search-bar @confirm="search" v-model="nameOrPhone" placeholder="请输入名称或手机号" @input="input" @cancel="cancel"></uni-search-bar>
			</view>
			<view style="height: 40rpx" />

			<uni-list-item v-for="item in policeList" :key="item.id" class="item-list-content">
				<template v-slot:header>
					<view class="flexrowcenter" @click="clickContontList(item.id)">
						<image v-if="checkDataList.includes(item.id)" class="checkboxicon" src="@/static/images/mine/checkbox-checked.png"></image>
						<image v-else class="checkboxicon" src="@/static/images/mine/checkbox-unchecked.png"></image>

						<view style="width: 10rpx" />
						<view class="flexcolumn">
							<text class="text-df">{{ item.name }}</text>
							<text class="text-sm text-gray margin-top-xs">{{ item.deptName }}</text>
						</view>
					</view>
				</template>
				<template v-slot:footer>
					<view class="flexrowcenter" @click="goToDetail(item)" @click.stop>
						<view class="flexcolumn">
							<text class="text-grey text-df">{{ `${item.phone} >` }}</text>
						</view>
					</view>
				</template>
			</uni-list-item>

			<view class="button-container">
				<button type="primary" class="button" v-if="auth('thing:responder:create')" @click="addOrModify">新增接警人</button>
				<button type="warn" class="button" v-if="auth('thing:responder:delete')" @click="deletePolice">删除接警人</button>
			</view>
		</z-paging>
	</view>
</template>

<script>
import { thingResponderPage, thingResponderDelete } from '@/api/police/index';
import dayjs from 'dayjs';

import authMixin from '@/mixin/auth.js';
export default {
	mixins: [authMixin],
	data() {
		return {
			checkDataList: [],
			policeList: [],
			nameOrPhone: ''
		};
	},
	methods: {
		goToDetail(item) {
			console.log('item>>>', item);
			uni.navigateTo({
				url: '/pages/mine/add-police/add-police?data=' + encodeURIComponent(JSON.stringify(item))
			});
			uni.$on('refresh-police-list', (e) => {
				if (e) {
					this.$refs.paging.reload();
				}
			});
		},
		queryList(pageNo, pageSize) {
			console.log('this.datetimerange>>', this.datetimerange);
			if (pageNo == 1) {
				uni.showLoading({
					title: '请求中...'
				});
			}
			thingResponderPage({
				size: pageSize,
				pages: pageNo,
				nameOrPhone: this.nameOrPhone
			}).then((res) => {
				uni.hideLoading();
				const records = res.data.list || []; // 读取 data 数据
				this.$refs.paging.complete(records);
			});
		},
		addOrModify() {
			uni.navigateTo({
				url: '/pages/mine/add-police/add-police?isAdd=1'
			});
			uni.$on('refresh-police-list', (e) => {
				if (e) {
					this.$refs.paging.reload();
				}
			});
		},
		search() {
			console.log('confirm>>>', this.nameOrPhone);
			// this.queryList(1,10)
			this.$refs.paging.refresh();
		},
		input(textInputData) {
			console.log('textInput>>input>', this.nameOrPhone);
		},
		cancel(cancelData) {
			console.log('cancelData>>cancel>', this.nameOrPhone);
		},
		clickContontList(id) {
			let checkDataListset = new Set(this.checkDataList);
			if (checkDataListset.has(id)) {
				checkDataListset.delete(id);
			} else {
				checkDataListset.add(id);
			}
			this.checkDataList = Array.from(checkDataListset);
		},
		deletePolice() {
			const deleteids = this.checkDataList;
			const deleteidsdesc = deleteids.join(',');
			thingResponderDelete({
				ids: deleteidsdesc
			}).then((res) => {
				if (res.data) {
					//删除成功
					console.log('%c [ res.data ]-129', 'font-size:13px; background:#6fa66f; color:#b3eab3;', res.data);
					this.$refs.paging.reload();
				}
			});
		}
	}
};
</script>

<style scoped>
page {
	background-color: #f5f6f7;
}
.content {
	width: 750px;
	display: flex;
	flex-direction: column;
	height: 100%;
	padding-bottom: calc(var(--window-bottom) + 20px);
	background-color: #f5f6f7;
}

.search-bar {
	width: 750rpx;
	background: #fff;
	border-radius: 20rpx;
	padding: 0 30rpx;
}
.checkboxicon {
	margin-right: 20rpx;
	height: 50rpx;
	width: 50rpx;
}

.left-cell-text {
	color: #333333;
	font-size: 14px;
}

.item-list-content {
	height: 120rpx;
	width: 750rpx;
}

.button-container {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 0 20rpx;
	margin-bottom: 40rpx;
	height: 50px;
	background-color: #f5f5f5;
}

.button {
	flex: 1;
	margin-right: 10px;
	margin-left: 10px;
	width: 30%;
	height: 100%;
	text-align: center;
	line-height: 50px;
	font-size: 16px;
	border-radius: 5px;

	color: #fff;
}
</style>
